package com.example.lcpharmacy.controller;

import com.example.lcpharmacy.entity.CartEntity;
import com.example.lcpharmacy.entity.UserEntity;
import com.example.lcpharmacy.entity.resBean.ShowCartInfo;
import com.example.lcpharmacy.mapper.CartDao;
import com.example.lcpharmacy.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartImpl {

    @Autowired
    private CartService cartService;

    @Autowired
    private CartDao cartDao;

    /**
     * 更新用户的购物车信息信息
     * @return
     */
    @RequestMapping("/saveCartModify")
    public int saveUserModify(@RequestBody CartEntity cartEntity) {
        int res = cartService.insertCart(cartEntity);
        return res;
    }

    /**
     * 查询用户的购物车信息
     * @return
     */
    @RequestMapping("/queryCart")
    public List<ShowCartInfo> queryCart(@RequestBody UserEntity userEntity) {
        String name = userEntity.getName();
        List<ShowCartInfo> showCartInfos = cartService.queryCart(name);
        return showCartInfos;
    }

    /**
     * 减少用户的购物车商品数量
     * @return
     */
    @RequestMapping("/subGoodCount")
    public int subGoodCount(@RequestBody CartEntity cartEntity) {
        int result = cartService.subGoodCount(cartEntity);
        return result;
    }

    /**
     * 增加用户的购物车商品数量
     * @return
     */
    @RequestMapping("/addGoodCount")
    public int addGoodCount(@RequestBody CartEntity cartEntity) {
        int result = cartDao.addGoodsQuan(cartEntity);
        return result;
    }

    /**
     * 更新购物车商品选定状态
     */
    @RequestMapping("/modifyStatus")
    public int modifyStatus(@RequestBody CartEntity cartEntity) {
        String currStatus = cartEntity.getStatus();
        if (currStatus.equals("Y")) {
            int result = cartDao.unsureStatus(cartEntity.getCartCode());
            return result;
        } else {
            int result = cartDao.sureStatus(cartEntity.getCartCode());
            return result;
        }
    }
}
